package com.ruoyi.report.controller;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.report.domain.entity.KyBaseTemplateEntity;
import com.ruoyi.report.service.IKyBaseTemplateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 报告模板Controller
 */
@Api(value = "report template interface")
@RestController
@RequestMapping("/base/template")
public class KyBaseTemplateController extends BaseController {
    @Resource
    private IKyBaseTemplateService kyBaseTemplateService;

    /**
     * 查询报告模板列表
     */
    @ApiOperation(value = "query report template list")
    @PreAuthorize("@ss.hasPermi('platform:template:list')")
    @GetMapping("/list")
    public TableDataInfo list(KyBaseTemplateEntity kyBaseTemplateEntity) {
        kyBaseTemplateEntity.setUserId(getUserId());
        kyBaseTemplateEntity.setDeptId(getDeptId());
        startPage();
        List<KyBaseTemplateEntity> list = kyBaseTemplateService.selectKyBaseTemplateList(kyBaseTemplateEntity);
        return getDataTable(list);

    }

    /**
     * 导出报告模板列表
     */
    @ApiOperation(value = "export report template list")
    @PreAuthorize("@ss.hasPermi('platform:template:export')")
    @Log(title = "报告模板", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, KyBaseTemplateEntity kyBaseTemplateEntity) {
        List<KyBaseTemplateEntity> list = kyBaseTemplateService.selectKyBaseTemplateList(kyBaseTemplateEntity);
        ExcelUtil<KyBaseTemplateEntity> util = new ExcelUtil<KyBaseTemplateEntity>(KyBaseTemplateEntity.class);
        util.exportExcel(response, list, "报告模板数据");
    }

    /**
     * 获取报告模板详细信息
     */
    @ApiOperation(value = "query template details")
    @PreAuthorize("@ss.hasPermi('platform:template:query')")
    @GetMapping(value = "/{tempId}")
    public AjaxResult getInfo(@PathVariable("tempId") Long tempId) {
        return success(kyBaseTemplateService.selectKyBaseTemplateByTempId(tempId));
    }

    /**
     * 新增报告模板
     */
    @ApiOperation(value = "add new template")
    @PreAuthorize("@ss.hasPermi('platform:template:add')")
    @Log(title = "报告模板", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody KyBaseTemplateEntity kyBaseTemplateEntity) {
        return toAjax(kyBaseTemplateService.insertKyBaseTemplate(kyBaseTemplateEntity));
    }

    /**
     * 修改报告模板
     */
    @ApiOperation(value = "update template")
    @PreAuthorize("@ss.hasPermi('platform:template:edit')")
    @Log(title = "报告模板", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody KyBaseTemplateEntity kyBaseTemplateEntity) {
        return toAjax(kyBaseTemplateService.updateKyBaseTemplate(kyBaseTemplateEntity));
    }

    /**
     * 删除报告模板
     */
    @PreAuthorize("@ss.hasPermi('platform:template:remove')")
    @ApiOperation(value = "delete template")
    @Log(title = "报告模板", businessType = BusinessType.DELETE)
    @DeleteMapping("/{tempIds}")
    public AjaxResult remove(@PathVariable Long[] tempIds) {
        return toAjax(kyBaseTemplateService.deleteKyBaseTemplateByTempIds(tempIds));
    }


    /**
     * 批量添加模板
     */
    @ApiOperation(value = "bulk add templates")
    @PreAuthorize("@ss.hasPermi('platform:template:add')")
    @Log(title = "批量上传报告模板", businessType = BusinessType.INSERT)
    @PutMapping(value = "batch-insert")
    public AjaxResult batchInsert(@RequestBody @NotNull String json) {

        if (json != null && json.length() != 0) {
            JSONObject jsonObject = JSON.parseObject(json);
            String tempType = String.valueOf(jsonObject.get("tempType"));
            String remark =  jsonObject.get("remark") == null ? "": (String) jsonObject.get("remark");
            JSONArray nameArray = jsonObject.getJSONArray("newFileNames");
            String belongFlag = (String) jsonObject.get("belongFlag");
            List<KyBaseTemplateEntity> kyBaseTemplateEntityList = new ArrayList<>();
            for (int i = 0; i < nameArray.size(); i++) {
                KyBaseTemplateEntity template = new KyBaseTemplateEntity();
                template.setDeptId(getDeptId());
                template.setUserId(getUserId());
                template.setTempName((String) nameArray.get(i));
                template.setTempType(tempType);
                template.setBelongFlag(belongFlag);
                template.setCreateBy(getUsername());
                template.setCreateTime(new Date());
                template.setRemark(remark);
                kyBaseTemplateEntityList.add(template);
            }
            return AjaxResult.success(kyBaseTemplateService.batchInsert(kyBaseTemplateEntityList));
        } else {
            return AjaxResult.success(0);
        }
    }

}
